package pl.veneo.wigprofit.view.elements {
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.text.TextFormat;
	import flash.text.TextFieldAutoSize;
	import flash.text.TextField;
	import pl.veneo.wigprofit.model.Configuration;
	import flash.display.Sprite;

	/**
	 * @author Tomasz Swierszcz, fiodorkrk@gmail.com
	 */
	public class LegendElement extends Sprite {
		
		public static const TOGGLE_CHART : String = "togglechart";
		
		private var tf1 : Sprite;
		private var tf2 : Sprite;
		
		public function LegendElement() {
			
			tf1 = addChild(new Sprite()) as Sprite;
			tf2 = addChild(new Sprite()) as Sprite;
			tf1.mouseChildren = false;
			tf2.mouseChildren = false;
			
			graphics.beginFill(0xFFFFFF,0.7);
			graphics.lineStyle(1,Configuration.getInstance().lightColor);
			graphics.drawRect(0, 0, 94, 33);
			tf1.graphics.lineStyle(2, Configuration.getInstance().indexColor);
			tf1.graphics.drawRect(6, 9, 8, 1);
			tf2.graphics.lineStyle(2, Configuration.getInstance().percentColor);
			tf2.graphics.drawRect(6, 22, 8, 1);
			
			var _tf1: TextField = new TextField();
			_tf1.autoSize = TextFieldAutoSize.LEFT;
			_tf1.defaultTextFormat = getStyleForIndex();
			_tf1.text = "dane WIG20";
			
			var _tf2: TextField = new TextField();
			_tf2.autoSize = TextFieldAutoSize.LEFT;
			_tf2.defaultTextFormat = getStyleForPercent();
			_tf2.text = "dane procentowe";
			
			tf1.addChild(_tf1);
			tf2.addChild(_tf2);
			
			_tf1.x = _tf2.x = 18;
			_tf1.y = 2;
			_tf2.y = 15;
			
			tf1.buttonMode = true;
			tf2.buttonMode = true;
			tf1.addEventListener(MouseEvent.CLICK, switchLegend);
			tf2.addEventListener(MouseEvent.CLICK, switchLegend);
			
		}
		
		public function get toggle1 () : Boolean {
		
			return tf1.alpha==1 ? true : false;
			
		}
		
		public function get toggle2 () : Boolean {
		
			return tf2.alpha==1 ? true : false;
			
		}

		private function switchLegend(event : MouseEvent) : void {
			
			switch(event.target){
				case tf1:
				tf1.alpha = tf1.alpha == 1 ? 0.3 : 1;
				break;
				case tf2:
				tf2.alpha = tf2.alpha == 1 ? 0.3 : 1;
				break;
			}
			
			dispatchEvent(new Event(LegendElement.TOGGLE_CHART));
			
		}
		
		private function getStyleForPercent() : TextFormat {
			var tf : TextFormat = new TextFormat();
			// tf.align = TextFormatAlign.RIGHT;
			tf.size = 9;
			tf.font = "Tahoma";
			tf.color = Configuration.getInstance().percentColor;
			return tf;
		}

		private function getStyleForIndex() : TextFormat {
			var tf : TextFormat = new TextFormat();
			// tf.align = TextFormatAlign.RIGHT;
			tf.size = 9;
			tf.font = "Tahoma";
			tf.color = Configuration.getInstance().indexColor;
			return tf;
		}
	}
}
